package com.mor.shiro.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zht
 * @version 1.0
 * @createDate 2020/07/12 21:25
 */

@Aspect
@Component
public class LogAspect {
//    @Pointcut( value = "execution(public * com.mor.shiro.controller.*.*(..)))")
    //自定义注解来解决
    @Pointcut( value = "@annotation(com.mor.shiro.test.SecondAnnotation)")
    public void log(){
    }
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(LogAspect.class);
    //统计请求的处理时间
    ThreadLocal<Long> startTime = new ThreadLocal<>();
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        try {
            Class c=Class.forName("com.mor.shiro.test.SecondAnnotation");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        startTime.set(System.currentTimeMillis());
    }
    @AfterReturning(returning = "ret" , pointcut = "log()")
    public void doAfterReturning(Object ret){
        //接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //记录请求的内容
        String url=request.getRequestURL().toString();

        System.out.println(url);
        System.out.println("Aspect_Method:"+request.getMethod());

        }

}
